import { HttpException, HttpStatus } from "@nestjs/common";

export const buildTree = (data, parentId = 0) => {
    return data.filter(item => item.parentId === parentId).map(item => ({
        ...item,
        children: buildTree(data, item.id).length > 0 ? buildTree(data, item.id) : null
    }));
}

export const throwCustomException = (message: string, status: number = HttpStatus.INTERNAL_SERVER_ERROR) => {
    throw new HttpException(message, status);
}

export const dateNo = ()=>{
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1
    const day = String(now.getDate()).padStart(2, '0');

    return `${year}${month}${day}`
}